<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-08-12
 * Time: 下午 8:54
 */
error_reporting(0);
ini_set('display_errors', 0);


if ($_POST && isset($_POST['password'])) {


    function formatDir($url)
    {
        $url = str_replace('\\', '/', $url);

        /*
        $url = preg_replace_callback('/([\/]{2,})/', function ($match) {
            //var_dump($match);
            return '/';
        }, $url);
        */
        $url = preg_replace('/([\/]{2,})/', '/', $url);
        return $url;
    }

    define('BASE_PATH', dirname(__FILE__));


    $ret = array(
        'code' => '1',
        'msg' => 'System Error'
    );
    if ('hashmap.cn' == $_POST['password']) {
        $subdir = isset($_POST['subdir']) && $_POST['subdir'] ? $_POST['subdir'] : 'images/alibaba';
        $subdir = formatDir('/' . $subdir . '/' . date('Ym/dH')) . '/';
        $save_path = formatDir(BASE_PATH . $subdir);

        $file = isset($_FILES['file']) ? $_FILES['file'] : array();
        var_dump($file);
        if ($file && isset($file['error'])) {
            /**
             * array (size=5)
             *      'name' => string 'd52a2834349b033b922043d017ce36d3d539bd14.jpg' (length=44)
             *      'type' => string 'image/jpeg' (length=10)
             *      'tmp_name' => string 'C:\Windows\php135B.tmp' (length=22)
             *      'error' => int 0
             *      'size' => int 42320
             */
            if (0 == $file['error']) {
                $file_name = $file['name'];
                $file_tmp_name = $file['tmp_name'];
                $file_ext = substr($file_name, intval(strrpos($file_name, '.')) + 1);
                $filename = uniqid(date('YmdH')) . '.' . $file_ext;
                //创建文件夹
                if (!is_dir($save_path)) {
                    mkdir($save_path, 0777, true);
                }
                //移动上传文件
                if (move_uploaded_file($file_tmp_name, $save_path . $filename)) {
                    $ret = array(
                        'code' => '0',
                        'msg' => 'Success',
                        'url' => $subdir . $filename
                    );
                } else {
                    $ret = array(
                        'code' => '5',
                        'msg' => 'Move File Fail'
                    );
                }

            } else {
                $ret = array(
                    'code' => '4',
                    'msg' => 'File Upload Error(' . $file['error'] . ')'
                );
            }
        } else {
            $ret = array(
                'code' => '3',
                'msg' => 'None File Upload'
            );
        }
    } else {
        $ret = array(
            'code' => '2',
            'msg' => 'Password Error'
        );
    }
    exit(json_encode($ret));
}


?>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文件上传</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
    <input type="text" placeholder="保存目录" value="images/alibaba" name="subdir">
    <input type="text" placeholder="验证密码：官方域名" name="password">
    <input type="file" name="file">
    <input type="submit" value="提交">
</form>
</body>
</html>

